package com.ruoyi.cms.service.impl;

import java.util.List;

import com.ruoyi.cms.dto.DisplayLogoDTO;
import com.ruoyi.cms.dto.DisplayVideoDTO;
import com.ruoyi.cms.vo.LogoVO;
import com.ruoyi.cms.vo.VideoVO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CDisplayMapper;
import com.ruoyi.cms.domain.CDisplay;
import com.ruoyi.cms.service.ICDisplayService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 展示屏管理Service业务层处理
 * 
 * @author Choi
 * @date 2021-08-23
 */
@Service
public class CDisplayServiceImpl implements ICDisplayService 
{
    @Autowired
    private CDisplayMapper cDisplayMapper;
    @Autowired
    private TokenService tokenService;

    @Override
    public void deleteDisplayVideo(DisplayVideoDTO displayVideoDTO) {
        cDisplayMapper.deleteDisplayVideo(displayVideoDTO);
    }

    @Override
    @Transactional
    public void addDisplayVideo(DisplayVideoDTO displayVideoDTO) {
        cDisplayMapper.deleteDisplayVideoById(displayVideoDTO.getDisplayId());
        cDisplayMapper.insertDisplayVideo(displayVideoDTO);
    }

    @Override
    public List<VideoVO> getDisplayVideos(long id) {
        List<VideoVO> displayVideo = cDisplayMapper.getDisplayVideoById(id);
        List<VideoVO> videos = cDisplayMapper.getDisplayVideos(id);
        displayVideo.addAll(videos);
        return displayVideo;
    }

    @Override
    public void editDisplayLogo(DisplayLogoDTO displayLogoDTO) {
        cDisplayMapper.updateDisplayLogo(displayLogoDTO);
    }

    @Override
    public void deleteDisplayLogo(long id, long[] logoIds) {
        cDisplayMapper.deleteDisplayLogo(id, logoIds);
    }

    @Override
    public void addDisplayLogo(List<DisplayLogoDTO> list) {
        cDisplayMapper.insertDisplayLogo(list);
    }

    @Override
    public List<LogoVO> getAllLogosById(long id) {
        return cDisplayMapper.getAllLogosById(id);
    }

    @Override
    public List<LogoVO> getLogosById(long id) {
        return cDisplayMapper.getLogosById(id);
    }

    /**
     * 查询展示屏管理
     * 
     * @param id 展示屏管理ID
     * @return 展示屏管理
     */
    @Override
    public CDisplay selectCDisplayById(Long id)
    {
        return cDisplayMapper.selectCDisplayById(id);
    }

    /**
     * 查询展示屏管理列表
     * 
     * @param cDisplay 展示屏管理
     * @return 展示屏管理
     */
    @Override
    public List<CDisplay> selectCDisplayList(CDisplay cDisplay)
    {
        cDisplay.setUserId(this.getUserId());
        return cDisplayMapper.selectCDisplayList(cDisplay);
    }

    /**
     * 新增展示屏管理
     * 
     * @param cDisplay 展示屏管理
     * @return 结果
     */
    @Override
    public int insertCDisplay(CDisplay cDisplay)
    {
        cDisplay.setUserId(this.getUserId());
        cDisplay.setCreateTime(DateUtils.getNowDate());
        return cDisplayMapper.insertCDisplay(cDisplay);
    }

    /**
     * 修改展示屏管理
     * 
     * @param cDisplay 展示屏管理
     * @return 结果
     */
    @Override
    public int updateCDisplay(CDisplay cDisplay)
    {
        cDisplay.setUpdateTime(DateUtils.getNowDate());
        return cDisplayMapper.updateCDisplay(cDisplay);
    }

    /**
     * 批量删除展示屏管理
     * 
     * @param ids 需要删除的展示屏管理ID
     * @return 结果
     */
    @Override
    public int deleteCDisplayByIds(Long[] ids)
    {
        return cDisplayMapper.deleteCDisplayByIds(ids);
    }

    /**
     * 删除展示屏管理信息
     * 
     * @param id 展示屏管理ID
     * @return 结果
     */
    @Override
    public int deleteCDisplayById(Long id)
    {
        return cDisplayMapper.deleteCDisplayById(id);
    }

    private SysUser getUser() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return null;
        }
        SysUser user = loginUser.getUser();
        return user;
    }

    private Long getUserId() {
        SysUser user = this.getUser();
        return user == null ? 0 : user.getUserId();
    }
}
